package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.MyEcharts;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/echarts")
public class EchartsController {
    @Autowired
    private OrderService orderService;

    @GetMapping
    public R<List<MyEcharts>> echarts(@RequestParam String startTime, @RequestParam String endTime) {
        System.err.println("报表查询");
        // 转换时间格式 Mon Oct 10 2022 00:00:00 GMT 0800 (香港标准时间) ===> Mon Oct 10 2022 00:00:00 GMT
        String[] startSplit = startTime.split(" ");
        String[] endSplit = endTime.split(" ");
        StringBuilder startSb = new StringBuilder();
        StringBuilder endSb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            startSb = startSb.append(startSplit[i] + " ");
            endSb = endSb.append(endSplit[i] + " ");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'", Locale.US);
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse(startSb.toString());
            date2 = sdf.parse(endSb.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 获取范围内的日期字符串
        List<String> dates = getDates(date1, date2);
        // 定义返回的集合
        List<MyEcharts> echarts = new ArrayList<>();
        // 根据每一天的日期查询当天的订单
        for (String date : dates) {
            LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
            lqw.like(Orders::getCheckoutTime, date);
            List<Orders> orders = orderService.list(lqw);
            Double amount = 0.0;
            for (Orders order : orders) {
                amount += order.getAmount().doubleValue();
            }
            // 定义返回集合中的对象 并赋予日期和当天的总收入
            MyEcharts echart = new MyEcharts();
            echart.setCheckoutTime(date);
            echart.setAmount(amount);
            echarts.add(echart);
        }
        return R.success(echarts);
    }

    /**
     * 根据日期范围返回范围内日期数组
     * @param startDate
     * @param endDate
     * @return
     */
    private List<String> getDates(Date startDate, Date endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 创建一个放所有日期的集合
        List<String> dates = new ArrayList();
        // 创建时间解析对象规定解析格式
        String startFormatTime = sdf.format(startDate);
        String endFormatTime = sdf.format(endDate);
        // 将传入的时间解析成Date类型,相当于格式化
        Date dStart = null;
        Date dEnd = null;
        try {
            dStart = sdf.parse(startFormatTime);
            dEnd = sdf.parse(endFormatTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 将格式化后的第一天添加进集合
        dates.add(sdf.format(dStart));
        // 使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
        // 传入起始时间将此日历设置为起始日历
        calBegin.setTime(dStart);
        // 判断结束日期是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            // 得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            // 如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }

}
